﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TransCode.Player.EntityViews;
using TransCode.Entitys;
using System.Linq;
using TransCode.Player.ImagePage;

namespace TransCode.Player
{
    /// <summary>
    /// TargetImageControl.xaml 的交互逻辑
    /// </summary>
    public partial class TargetImageControl : UserControl
    {
        static DependencyProperty VideoSourceProperty;

        public TargetImageControl()
        {
            this.InitializeComponent();
        }

        static Grid layoutgrid;

        static TargetImageControl()
        {
            VideoSourceProperty = DependencyProperty.Register(
                "TargetViewSource", typeof(List<Video>), typeof(TargetImageControl),
                new PropertyMetadata(null,
                    (DependencyObject d, DependencyPropertyChangedEventArgs e) =>
                    {
                        TargetImageControl ui = d as TargetImageControl;

                        ui.LayoutRoot.Children.Clear();

                        foreach (var video in e.NewValue as List<Video>)
                        {
                            TargetImageItem tii = new TargetImageItem();
                            tii.VideoSource = video;

                            Grid grid = new Grid();

                            ui.LayoutRoot.Children.Add(tii);
                        }

                        layoutgrid = ui.LayoutGrid;
                    })); 
        }

        static void listbox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (e.AddedItems.Count > 0)
            {
                ImageBoxControl iBox = new ImageBoxControl();
                TargetView view = e.AddedItems[0] as TargetView;
                iBox.TargetSource = view.Target.ObjectList;
                layoutgrid.Children.Add(iBox);
            }

            (sender as ListBox).SelectedIndex = -1;
        }

        public List<Video> VideoSource
        {
            get { return this.GetValue(TargetImageControl.VideoSourceProperty) as List<Video>; }
            set { SetValue(TargetImageControl.VideoSourceProperty, value); }
        }
    }
}